/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.campaign_service;

import java.util.ArrayList;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.Converter;
import com.hiddenequity.utils.SOAPUtils;
import com.hiddenequity.utils.UrlBuncher;


public class CampaignService {

	public static final String URL_CAMPAIGN_SERVICE = UrlBuncher.URL_BASE + "CampaignService";
	
	/*
	 * Get Campaign List
	 */
	static public String getCampaignList(ArrayList<String> dateRange, int status){

		String SOAPBody =
				"<soap:Body>" +
					"<get>" +
						"<serviceSelector>" +
							"<fields>Id</fields>" +
							"<fields>Name</fields>" +
							"<fields>Status</fields>" +
							"<fields>BiddingStrategy</fields>" +
							"<fields>Clicks</fields>" +
							"<fields>Ctr</fields>" +
							"<fields>Cost</fields>" +
							"<fields>Conversions</fields>" +
							"<fields>Impressions</fields>" +
							"<fields>Amount</fields>";

		if(status != 0){
			String statusName = "";
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ACTIVE";
					break;
				case Act_Super.STATUS_PAUSED:
					statusName = "PAUSED";
					break;
				case Act_Super.STATUS_DELETED:
					statusName = "DELETED";
					break;
			}
			
			SOAPBody = SOAPBody + 
				"<predicates>" +
					"<field>Status</field>" +
					"<operator>EQUALS</operator>" +
					"<values>" + statusName + "</values>" +
				"</predicates>";
		}
		
		
		if(!dateRange.get(0).equals("")){
			SOAPBody = SOAPBody +	"<dateRange>" +
						"<min>" + dateRange.get(0) +"</min>" +
						"<max>" + dateRange.get(1) +"</max>" +
						"</dateRange>";
		}
							
		SOAPBody = SOAPBody + 
							"<ordering>" +
								"<field>Clicks</field>" +
								"<sortOrder>DESCENDING</sortOrder>" +
							"</ordering>" +
						"</serviceSelector>" +
					"</get>" +
				"</soap:Body>";		

		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//getCampaignList
	
	
	
	/*
	 * Get Campaign By Id
	 */
	static public String getCampaignById(String campaignId){
		
		String SOAPBody =
				"<soap:Body>" +
					"<get>" +
						"<serviceSelector>" +
							"<fields>Id</fields>" +
							"<fields>Name</fields>" +
							"<fields>Status</fields>" +
							"<fields>Amount</fields>" +
							"<fields>BiddingStrategy</fields>" +
							"<fields>BidCeiling</fields>" +
							"<fields>TargetGoogleSearch</fields>" +
							"<fields>TargetSearchNetwork</fields>" +
							"<fields>TargetContentNetwork</fields>" +
							"<fields>TargetContentContextual</fields>" +
							"<fields>TargetPartnerSearchNetwork</fields>" +
							"<fields>DeliveryMethod</fields>" +
							"<fields>AdServingOptimizationStatus</fields>" +
							"<predicates>" +
								"<field>Id</field>" +
								"<operator>EQUALS</operator>" +
								"<values>" + campaignId + "</values>" +
							"</predicates>" +
						"</serviceSelector>" +
					"</get>" +
				"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//getCampaignById	
	

	
	/*
	 * Add or Set Campaign depends on provided id
	 * if id is 0 its mean ADD operation, SET otherwise
	 */
	static public String campaignOperation(
			String campaignId,			
			final String name,  
			String budget, 
			boolean manualCpc, 
			String bidLimit,
			boolean netGoogle,
			boolean netPartner,
			boolean netDisplay,
			boolean netBroadest,
			boolean netAll,
			boolean transitionFlag,
			boolean deliveryMethod, 
			boolean adServingOptimizationStatus){
		
		// Ad rotation tag
		String adServingOptimizationStatusTag = adServingOptimizationStatus ? 
				"<adServingOptimizationStatus>OPTIMIZE</adServingOptimizationStatus>" : 
					"<adServingOptimizationStatus>ROTATE</adServingOptimizationStatus>";
		
		// Delivery method tag
		String deliveryMethodTag = deliveryMethod ? 
				"<deliveryMethod>STANDARD</deliveryMethod>" : 
					(manualCpc == false ? "<deliveryMethod>STANDARD</deliveryMethod>" : 
						"<deliveryMethod>ACCELERATED</deliveryMethod>");
		
		//set Operation name and bidding strategy according to operator
		String operator = "";
		String campaignIdTag = "";
		String biddingTag = "";
		String biddingTransition = "";
		
		if(campaignId == null){
			operator = "ADD";
		} else {
			operator = "SET";
			campaignIdTag = "<id>" + campaignId + "</id>";	
		}
	
		//Set bidding settings
		if(operator.equals("SET") && transitionFlag){
			if(manualCpc){
				biddingTransition = "<biddingTransition>" +
						"<targetBiddingStrategy xsi:type='ManualCPC'/>" +
						"</biddingTransition>";
			} else {
				biddingTransition = "<biddingTransition>" +
						"<targetBiddingStrategy xsi:type='BudgetOptimizer'>";
				if(bidLimit !=null){
					biddingTransition = biddingTransition + 
							"<bidCeiling><microAmount>" +
							Converter.strToMicroamount(bidLimit) + 
							"</microAmount></bidCeiling>";
				}
				biddingTransition = biddingTransition + 
						"</targetBiddingStrategy></biddingTransition>";
			}	
		} else {
			if(manualCpc){
				biddingTag = "<biddingStrategy xsi:type='ManualCPC'/>";
			} else {
				biddingTag = "<biddingStrategy xsi:type='BudgetOptimizer'>";
				if(bidLimit !=null){
					biddingTag = biddingTag + 
							"<bidCeiling><microAmount>" + 
							Converter.strToMicroamount(bidLimit) + 
							"</microAmount></bidCeiling>";
				}
				biddingTag = biddingTag + "</biddingStrategy>";
			}			
		}
		
		
		//Build network setting tag
		String networkSettingsTag = "";
			networkSettingsTag = 
				"<networkSetting>" +
					"<targetGoogleSearch>" + netGoogle + "</targetGoogleSearch>" +
					"<targetSearchNetwork>" + netPartner + "</targetSearchNetwork>";
			if(netBroadest){
				networkSettingsTag = networkSettingsTag + 
					"<targetContentNetwork>true</targetContentNetwork>" +
					"<targetContentContextual>true</targetContentContextual>";				
			} else if(netAll){
				networkSettingsTag = networkSettingsTag + 
					"<targetContentNetwork>true</targetContentNetwork>" +
					"<targetContentContextual>false</targetContentContextual>";			
			} else {
				networkSettingsTag = networkSettingsTag + 
					"<targetContentNetwork>false</targetContentNetwork>" +
					"<targetContentContextual>false</targetContentContextual>";	
			}
			networkSettingsTag = networkSettingsTag + 
				"</networkSetting>";

		String SOAPBody =
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						" xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<operations>" +
							"<operator>" + operator + "</operator>" +
							biddingTransition +
							"<operand>" +
								campaignIdTag + 
								"<name>" + Converter.escapeXML(name) + "</name>" +
								"<budget>" +
									"<period>DAILY</period>" +
									"<amount>" +
										"<microAmount>" + Converter.strToMicroamount(budget) + "</microAmount>" +
									"</amount>" +
									deliveryMethodTag +
								"</budget>" +
								biddingTag +
								
								adServingOptimizationStatusTag +
								
								networkSettingsTag +
							"</operand>" +
						"</operations>" +
					"</mutate>" +
				"</soap:Body>";
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
		
	}//campaignOperation
	


	/*
	 * Set campaign status
	 */
	static public String setCampaignStatus(String campaignId, int status){
		String statusName = "";
		
		if(status != 0){
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ACTIVE";
					break;
				case Act_Super.STATUS_PAUSED:
					statusName = "PAUSED";
					break;
				case Act_Super.STATUS_DELETED:
					statusName = "DELETED";
					break;
			}
		}
		
		String SOAPBody = 
			"<soap:Body>" +
				"<mutate xmlns=" + SOAPUtils.XMLNS +
					" xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>SET</operator>" +
						"<operand>" +
							"<id>" + campaignId + "</id>" +
							"<status>"+ statusName + "</status>" +
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;
	}//setCampaignStatus

}
